package com.example.studentgradesmanagement.controller;

import com.example.studentgradesmanagement.model.Grade;
import com.example.studentgradesmanagement.service.GradeService;
import com.example.studentgradesmanagement.util.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/grades")
public class GradeController {
    @Autowired
    private GradeService gradeService;

    @GetMapping("/listAll")
    public ResponseEntity<List<Grade>> listAll() {
        List<Grade> grades = gradeService.listAll();
        return ResponseEntity.ok(grades);
    }

    @GetMapping("/getByCourseId/{courseId}")
    public ResponseEntity<ApiResponse> getByCourseId(@PathVariable Long courseId) {
        List<Grade> grades = gradeService.getByCourseId(courseId);
        if (!grades.isEmpty()) {
            return ResponseEntity.ok(new ApiResponse(true, "Grades found", grades));
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @GetMapping("/getByRange/{minScore}/{maxScore}")
    public ResponseEntity<ApiResponse> getByRange(@PathVariable double minScore, @PathVariable double maxScore) {
        List<Grade> grades = gradeService.getByRange(minScore, maxScore);
        if (!grades.isEmpty()) {
            return ResponseEntity.ok(new ApiResponse(true, "Grades found", grades));
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @PostMapping("/update")
    public ResponseEntity<ApiResponse> update(@RequestBody Grade grade) {
        gradeService.update(grade);
        return ResponseEntity.ok(new ApiResponse(true, "Grade updated successfully", grade));
    }
}
